<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>run.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="local_closure_goog_async_run.js.html">run.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2013 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>goog.provide(&#39;goog.async.run&#39;);
<a name="line16"></a>goog.provide(&#39;goog.async.throwException&#39;);
<a name="line17"></a>
<a name="line18"></a>goog.require(&#39;goog.async.nextTick&#39;);
<a name="line19"></a>goog.require(&#39;goog.testing.watchers&#39;);
<a name="line20"></a>
<a name="line21"></a>
<a name="line22"></a>/**
<a name="line23"></a> * Throw an item without interrupting the current execution context.  For
<a name="line24"></a> * example, if processing a group of items in a loop, sometimes it is useful
<a name="line25"></a> * to report an error while still allowing the rest of the batch to be
<a name="line26"></a> * processed.
<a name="line27"></a> * @param {*} exception
<a name="line28"></a> */
<a name="line29"></a>goog.async.throwException = function(exception) {
<a name="line30"></a>  // Each throw needs to be in its own context.
<a name="line31"></a>  goog.async.nextTick(function() { throw exception; });
<a name="line32"></a>};
<a name="line33"></a>
<a name="line34"></a>
<a name="line35"></a>/**
<a name="line36"></a> * Fires the provided callback just before the current callstack unwinds, or as
<a name="line37"></a> * soon as possible after the current JS execution context.
<a name="line38"></a> * @param {function(this:THIS)} callback
<a name="line39"></a> * @param {THIS=} opt_context Object to use as the &quot;this value&quot; when calling
<a name="line40"></a> *     the provided function.
<a name="line41"></a> * @template THIS
<a name="line42"></a> */
<a name="line43"></a>goog.async.run = function(callback, opt_context) {
<a name="line44"></a>  if (!goog.async.run.workQueueScheduled_) {
<a name="line45"></a>    // Nothing is currently scheduled, schedule it now.
<a name="line46"></a>    goog.async.nextTick(goog.async.run.processWorkQueue);
<a name="line47"></a>    goog.async.run.workQueueScheduled_ = true;
<a name="line48"></a>  }
<a name="line49"></a>
<a name="line50"></a>  goog.async.run.workQueue_.push(
<a name="line51"></a>      new goog.async.run.WorkItem_(callback, opt_context));
<a name="line52"></a>};
<a name="line53"></a>
<a name="line54"></a>
<a name="line55"></a>/** @private {boolean} */
<a name="line56"></a>goog.async.run.workQueueScheduled_ = false;
<a name="line57"></a>
<a name="line58"></a>
<a name="line59"></a>/** @private {!Array.&lt;!goog.async.run.WorkItem_&gt;} */
<a name="line60"></a>goog.async.run.workQueue_ = [];
<a name="line61"></a>
<a name="line62"></a>
<a name="line63"></a>if (goog.DEBUG) {
<a name="line64"></a>  /**
<a name="line65"></a>   * Reset the event queue.
<a name="line66"></a>   * @private
<a name="line67"></a>   */
<a name="line68"></a>  goog.async.run.resetQueue_ = function() {
<a name="line69"></a>    goog.async.run.workQueueScheduled_ = false;
<a name="line70"></a>    goog.async.run.workQueue_ = [];
<a name="line71"></a>  };
<a name="line72"></a>
<a name="line73"></a>  // If there is a clock implemenation in use for testing
<a name="line74"></a>  // and it is reset, reset the queue.
<a name="line75"></a>  goog.testing.watchers.watchClockReset(goog.async.run.resetQueue_);
<a name="line76"></a>}
<a name="line77"></a>
<a name="line78"></a>
<a name="line79"></a>/**
<a name="line80"></a> * Run any pending goog.async.run work items. This function is not intended
<a name="line81"></a> * for general use, but for use by entry point handlers to run items ahead of
<a name="line82"></a> * goog.async.nextTick.
<a name="line83"></a> */
<a name="line84"></a>goog.async.run.processWorkQueue = function() {
<a name="line85"></a>  // NOTE: additional work queue items may be pushed while processing.
<a name="line86"></a>  while (goog.async.run.workQueue_.length) {
<a name="line87"></a>    // Don&#39;t let the work queue grow indefinitely.
<a name="line88"></a>    var workItems = goog.async.run.workQueue_;
<a name="line89"></a>    goog.async.run.workQueue_ = [];
<a name="line90"></a>    for (var i = 0; i &lt; workItems.length; i++) {
<a name="line91"></a>      var workItem = workItems[i];
<a name="line92"></a>      try {
<a name="line93"></a>        workItem.fn.call(workItem.scope);
<a name="line94"></a>      } catch (e) {
<a name="line95"></a>        goog.async.throwException(e);
<a name="line96"></a>      }
<a name="line97"></a>    }
<a name="line98"></a>  }
<a name="line99"></a>
<a name="line100"></a>  // There are no more work items, reset the work queue.
<a name="line101"></a>  goog.async.run.workQueueScheduled_ = false;
<a name="line102"></a>};
<a name="line103"></a>
<a name="line104"></a>
<a name="line105"></a>
<a name="line106"></a>/**
<a name="line107"></a> * @constructor
<a name="line108"></a> * @final
<a name="line109"></a> * @struct
<a name="line110"></a> * @private
<a name="line111"></a> *
<a name="line112"></a> * @param {function()} fn
<a name="line113"></a> * @param {Object|null|undefined} scope
<a name="line114"></a> */
<a name="line115"></a>goog.async.run.WorkItem_ = function(fn, scope) {
<a name="line116"></a>  /** @const */ this.fn = fn;
<a name="line117"></a>  /** @const */ this.scope = scope;
<a name="line118"></a>};
</pre>


</body>
</html>
